package ntd.util.frame.server.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import ntd.util.frame.server.ServerManager;
import ntd.util.frame.server.context.ContextServer;
import ntd.util.frame.server.util.UtilServer;
import ntd.util.modular.data.entity.DataAcquisitionRule;
import ntd.util.modular.data.service.DataAcquisitionRuleService;
import ntd.util.modular.data.util.UtilDataAcquisitionManager;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class ServerManagerImpl implements ServerManager {

    @Resource
    private DataAcquisitionRuleService dataAcquisitionRuleService;

    @Override
    public void init(ContextServer contextServer) {
        UtilServer.closeServer(contextServer.getCode());
        UtilServer.init(contextServer);
        dataAcquisitionRuleService
                .list(generatorQuery(contextServer.getCode()))
                .forEach(UtilDataAcquisitionManager::handleRule);
    }

    private QueryWrapper<DataAcquisitionRule> generatorQuery(String serverAppId) {
        QueryWrapper<DataAcquisitionRule> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("JSON_EXTRACT(extend_json, '$.serverAppId')", serverAppId);
        return queryWrapper;
    }
}
